package com.battle.service.controller.web;

import com.battle.pojo.param.user.*;
import com.battle.pojo.vo.organization.GetOrganizationDownListVo;
import com.battle.pojo.vo.user.*;
import com.battle.service.service.in.*;
import nirvana.core.context.WebContext;
import nirvana.core.domains.vo.PageResult;
import nirvana.core.domains.vo.ApiResult;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * web/用户
 * 【描 述】：
 * 【环 境】：J2SE
 * 【详 细】：
 */
@RestController
@RequestMapping("/user/web")
public class UserWebController {

    private final UserService service;

    public UserWebController(UserService service) {
        this.service = service;
    }


    /**
     * 创建用户
     *
     * @param param
     * @return
     */
    @PostMapping("/createUser")
    public ApiResult createUser(@RequestBody @Valid CreateUserParam param) {
        service.createUser(param);
        return ApiResult.ok();
    }


    /**
     * 修改用户
     *
     * @param param
     */
    @PutMapping("/editUser")
    public ApiResult editUser(@RequestBody @Valid EditUserParam param) {
        service.editUser(param);
        return ApiResult.ok();
    }

    /**
     * 删除用户
     *
     * @param id
     */
    @GetMapping("/removeUser")
    public ApiResult removeUser(@RequestParam("id") Long id) {
        service.removeUser(id);
        return ApiResult.ok();
    }

    /**
     * 重置密码
     *
     * @param id
     * @return
     */
    @GetMapping("/resetPassWordUser")
    public ApiResult resetPassWordUser(@RequestParam("id") Long id) {
        service.resetPassWordUser(id);
        return ApiResult.ok();
    }

    /**
     * 启动/禁用用户
     *
     * @param id
     */
    @GetMapping("/enabledUser")
    public ApiResult enabledUser(@RequestParam("id") Long id) {
        service.enabledUser(id);
        return ApiResult.ok();
    }

    /**
     * 获取用户详情
     *
     * @param id
     * @return
     */
    @GetMapping("/getUser")
    public ApiResult<GetUserVo> getUser(@RequestParam("id") Long id) {
        GetUserVo vo = service.getUser(id);
        return ApiResult.ok(vo);
    }

    /**
     * 获取当前用户信息
     *
     * @param
     * @return
     */
    @GetMapping("/getCurrentUser")
    public ApiResult<GetUserVo> getCurrentUser() {
        GetUserVo vo = service.getUser(WebContext.getCurrentUserId());
        return ApiResult.ok(vo);
    }

    /**
     * 获取用户列表
     *
     * @param param
     * @return
     */
    @PostMapping("/getUserList")
    public ApiResult<PageResult<GetUserListVo>> getUserList(@RequestBody GetUserListParam param) {
        PageResult<GetUserListVo> result = service.getUserList(param);
        return ApiResult.ok(result);
    }


    /**
     * 获取用户下拉列表
     *
     * @return
     */
    @PostMapping("/getUserDownList")
    public ApiResult<List<GetUserDownListVo>> getUserDownList(@RequestBody(required = false) @Valid GetUserDownListParam param) {
        List<GetUserDownListVo> list = service.getUserDownList(param);
        return ApiResult.ok(list);
    }

    /**
     * 获取当前用户的组织架构信息
     * @return
     */
    @GetMapping("/getOrganizationByCurrentUserId")
    public ApiResult<List<GetOrganizationDownListVo>> getOrganizationByCurrentUserId() {
        List<GetOrganizationDownListVo> list = service.getOrganizationByCurrentUserId();
        return ApiResult.ok(list);
    }

    /**
     * 导入用户
     *
     * @param file
     * @return
     */
    @PostMapping("/importUser")
    public void importUser(@RequestParam("file") MultipartFile file) {
        service.importUser(file);
    }



    /**
     * 修改密码
     *
     * @param param
     */
    @PostMapping("/updatePassword")
    public ApiResult updatePassword(@RequestBody @Valid UpdatePasswordParam param) {
        service.updatePassword(param);
        return ApiResult.ok();
    }

}

